<?php


namespace App\Tools;


use App\Models\PushLevel;
use App\Models\SystemConfig;

class FindWhere
{
    public function __construct()
    {

    }

    /**
     * 根据金额订单金额筛选等级
     * @param $pushUser
     * @param $tasks
     * @return mixed
     */
    public function findLevel($pushUser, $tasks)
    {
        // 根据金额筛选等级
        if ($tasks['plan_type'] == 1) {
            $shouldPay = bcmul($tasks['pay_money'], $tasks['buy_num']);
            $pushLevelList = PushLevel::get();
            $levels = [];
            if (!empty($pushLevelList)) {
                foreach ($pushLevelList as $value) {
                    if (bccomp($shouldPay, $value->min_money) >= 0 && bccomp($shouldPay, $value->max_money) <= 0) {
                        $levels[] = $value->id;
                    }
                }
            }
            $pushUser->whereIn('level', $levels);
        }
        return $pushUser;
    }

    /**
     * 查询标签
     * @param $pushUser
     * @param $tasks
     * @return mixed
     */
    public function findTags($pushUser, $tasks)
    {
        if (SystemConfig::findValue($tasks['plan_type'] == 1 ? 'dispatch.taglimit' : 'dispatch.flowtaglimit') == 1) {
            $planInfo = $tasks['$planInfo'];
            // 性别
            if (isset($planInfo['sex_id']) && $planInfo['sex_id'] != '' && $planInfo['sex_id'] != '99' && $planInfo['sex_id'] != 0) {
                $pushUser->where('sex', $planInfo['sex_id']);
            }
            // 信用
            if (isset($planInfo['credit_id']) && $planInfo['credit_id'] != '' && $planInfo['credit_id'] != '99' && $planInfo['credit_id'] != 0) {
                $pushUser->where('credit', $planInfo['credit_id']);
            }
            // 系统
            if (isset($planInfo['model_id']) && $planInfo['model_id'] != '' && $planInfo['model_id'] != '99' && $planInfo['model_id'] != 0) {
                $pushUser->where('system', $planInfo['model_id']);
            }
            //地区
            if (isset($planInfo['placement_area']) && !empty(trim($planInfo['placement_area'], ' '))) {
                $type = substr($planInfo['placement_area'], 0, 1);
                $placeStr = substr($planInfo['placement_area'], 1);
                $placeArray = explode(',', $placeStr);
                if (!empty($placeArray)) {
                    // 选中地区
                    if ($type == '+') {
                        $pushUser->whereIn('province', $placeArray);
                    }elseif ($type == '-') {
                        $pushUser->whereNotIn('province', $placeArray);
                    }
                }
            }
            // 年龄
            if (isset($planInfo['age_id']) && $planInfo['age_id'] != '' && $planInfo['age_id'] != '99' && $planInfo['age_id'] != 0) {
                $isAgeLimit = SystemConfig::findValue($tasks['plan_type'] == 1 ? 'dispatch.agelimit' : 'dispatch.flowagelimit', 1) == 1;
                $ageArray = explode(',', $planInfo['age_id']);
                if ($isAgeLimit && !empty($ageArray)) {
                    $pushUser->whereIn('age', $ageArray);
                }
            }
        }
        return $pushUser;
    }

}
